package org.tloss.web.editor.utils;

import java.util.ArrayList;
import java.util.List;

import org.tloss.web.editor.database.editors.jdbc.Column;
import org.tloss.web.editor.model.oper.math.EntityOperand;
import org.tloss.web.editor.model.oper.math.MathOperand;
import org.tloss.web.editor.model.variable.EntityCommandVariable;

public class EntityVariable implements Variable {
	private Variable parent;
	private EntityCommandVariable entityCommandVariable;
	private List<Variable> variables;

	public EntityCommandVariable getTable() {
		return entityCommandVariable;
	}

	public EntityVariable(EntityCommandVariable table) {
		this.entityCommandVariable = table;
	}

	@Override
	public List<Variable> getChildren() {
		if (entityCommandVariable != null) {
			if (variables == null) {
				variables = new ArrayList<Variable>();
			}
			variables.clear();
			List<Column> columns = entityCommandVariable.getTable().getColumns();
			if (columns != null) {
				for (int i = 0; i < columns.size(); i++) {
					ColumnVariable columnVariable = new ColumnVariable(
							columns.get(i), this);
					variables.add(columnVariable);
				}
			}
		}
		return variables;
	}

	@Override
	public Variable getParent() {
		return parent;
	}

	@Override
	public void setParent(Variable parent) {
		this.parent = parent;

	}

	@Override
	public boolean hasChildren() {
		return true;
	}

	@Override
	public void setChildren(List<Variable> variables) {
	}

	@Override
	public String toString() {
		return "" + entityCommandVariable.getName() + "[" + entityCommandVariable.getTable().getTableName()
				+ "]";
	}

	@Override
	public MathOperand convertToMathOperand() {
		EntityOperand operand =  new EntityOperand();
		operand.setTable(entityCommandVariable.getTable());
		operand.setName(entityCommandVariable.getName());
		return operand;
	}
}
